<?php
namespace app\common\model;

class OperateConvert extends Base
{
	static $status = [
            0 => '待处理',
            1 => '处理完成',
            2 => '处理失败',
            3 => '处理中'
        ];
    public static function status($key=null){
        if(!is_null($key)){
            return self::$status[$key];
        }else{
            return self::$status;
        }
    }

	protected $readonly = ['id'];
	protected $auto=[];
	protected $insert=['create_time'];

	protected function setCreateTimeAttr(){
		return time();
	}

	/**
     * 获取分页列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public function getPageDatas($where,$page,$perpagenum){
        
        $start = ($page-1)*$perpagenum;
        
        $whereData=[];
        if(!empty($where)){
            foreach($where as $k => $wh):
                $whereData['a.'.$k] = $wh;
            endforeach;
        }
        
        $rows = $this
                ->alias('a')
                ->join('member b','a.uid=b.id')
                ->where($whereData)
                ->field('a.*,b.mid mid')
                ->limit($start,$perpagenum)
                ->order('a.id','DESC')->select();

        $count = $this->alias('a')->where($whereData)->count();
        $data = [];
        if($count){
            foreach($rows as $row):
                $row = $row->toArray();
                $data[] = $this->setField($row);
            endforeach;
        }

        $total_page = ceil($count/$perpagenum);
        return [
            'total_page' => $total_page,
            'curent_page' => $page,
            'count' => $count,
            'data' => $data
        ];
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setField($row){
        if(isset($row['coin_id'])){
            $row['coin_name'] = Coin::coinData($row['coin_id'])['coin_name'];
        }
        if(isset($row['status'])){
            $row['status_remark'] = self::$status[$row['status']];
        }
        if(isset($row['convert_coin_id'])){
            $row['convert_coin_name'] = Coin::coinData($row['convert_coin_id'])['coin_name'];
        }
        if(isset($row['create_time'])){
            $row['create_date'] = date("Y-m-d H:i",$row['create_time']);
        }
        return $row;
    }

}
